package com.yx.cloud.utils;

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.airbnb.lottie.LottieAnimationView;
import com.yx.cloud.R;
import com.yx.cloud.view.CustomEditBottonDialog;


/**
 * 圆形加载框（菊花转）
 * 使用方式：
 * //                创建LoadProgressDialog（圆形加载框）
 * //                LoadProgressDialog loadProgressDialog=new LoadProgressDialog(this,"加载中……");
 * //                //显示
 * //                loadProgressDialog.show();
 * //                //更改message
 * //                loadProgressDialog.setMessage("加载数据中……");
 * //                //销毁
 * //                loadProgressDialog.dismiss();
 */
public class LoadProgressDialog extends Dialog implements Animator.AnimatorListener {
    private boolean canCancel;
    private LottieAnimationView lottieAnimationView;
    private Animator.AnimatorListener listener;
    private CustomEditBottonDialog.CallBackValue callBackValue = null;
    private int animatorRepeatCount = 3;
    private Activity mContext;

    public LoadProgressDialog(Context context) {
        this(context, true);
        mContext = (Activity) context;
    }

    public LoadProgressDialog(Context context, int repeatCount) {
        this(context, true);
        animatorRepeatCount = repeatCount;
        mContext = (Activity) context;
    }

    public LoadProgressDialog(Context context, boolean canCancel) {
        super(context, R.style.LoadProgressDialog);
        this.canCancel = canCancel;
        mContext = (Activity) context;
    }

    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading);
        lottieAnimationView = findViewById(R.id.lottieAnimationView);
        lottieAnimationView.addAnimatorListener(this);
        lottieAnimationView.setVisibility(View.VISIBLE);
        lottieAnimationView.setRepeatCount(animatorRepeatCount);

        setCanceledOnTouchOutside(canCancel);
        setCancelable(false);
        Log.d("test", "create loading");
    }

    public void addAnimatorListener(Animator.AnimatorListener listener) {
        this.listener = listener;
    }

    @SuppressLint("RestrictedApi")
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }

    @Override
    public void onAnimationStart(Animator animation) {
        if (listener != null) {
            listener.onAnimationStart(animation);
        }

    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if (listener != null) {
            listener.onAnimationEnd(animation);
        }
        dismiss();
        Log.d("test", "end dismiss");
    }

    @Override
    public void onAnimationCancel(Animator animation) {
        if (listener != null) {
            listener.onAnimationCancel(animation);
        }
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
        if (listener != null) {
            listener.onAnimationRepeat(animation);
        }
    }

    @Override
    public void show() {
        boolean scale = AnimationtUtils.isAnimatorDurationScale(mContext);
        if (!scale) {
            if (!mContext.isFinishing())
                super.show();
        }
    }

    @Override
    public void dismiss() {
        if (!mContext.isFinishing())
            super.dismiss();
    }
}

